<?php
	include 'commonAccountFunctions.php';
	
	// global variable for user accounts
	$useraccts = array();
	
	session_start();
	$user = checkAuthentication();
	
	if(!(getPermissions($user)=="labman")){
		header('Location: controlPanel.php');
	}
	
	print_header();
?>
	<div class="header">
			UserName: <?= $user ?>
			<form action="index.php">
				<button type='submit' class="unButtons">Log Out</button>
			</form>
			<form action="controlpanel.php">
				<button type='submit' class="unButtons">Back</button>
			</form>
	</div>
		
	<form action="createAccount.php" method="post">
		<div class="forms">
			<h3> Create New User: </h1>
			<p id="user"> Username: <input type="text" name="userName"  /> </p>
			<!--<button type="button" id="create">test</button>-->
			</br>
			<p id="pwd"> Password: <input type="password" name="password" /> </p>
			</br>
			<p id="vpwd"> Verify Password: <input type="password" name="verifyPassword" /> </p>
			</br>
			<p> E-mail: <input type="text" id="iEmail" name="emailAddress" /> </p>
			<p id="email"></p>
			</br>
			<p> Verify E-mail: <input type="text" id="ivEmail" name="verifyEmail" /> </p>
			<p id="vemail"></p>
			</br>
			Authorization: 
			<select name="authorization">
				<option value="labins">Inspector</option>
				<option value="labaud">Auditor</option>
				<option value="labman">Lab Manager</option>
			</select>
			<br/>
			<input type="submit" />
		</div>
	</form>
		
		<div class="forms">
			<form action="deleteAccount.php" method="post">
				<div id="deleteArea">
					<h3> Delete Account: </h3>
					<p> UserName: <input type="text" name="username" /> </p>
					</br>
					<button>Delete</button>
				</div>
			</form>
			<div id="userList">
				<h3> List Of Users: </h3>
				<ol>
<?php
		populateUseraccts();
		$test=validateUsers($useraccts);
		
		foreach($test as $element){
			$username=explode(".", $element);
			print "<li>$username[0]</li>";
		}
?>
				</ol>
			</div>
		</div>
<?php
	print_footer();
	
	// void -> void
	// fills the useraccts global array with the txt files associated
	// with the users
	function populateUseraccts(){
		global $useraccts;
		// source: http://php.net/manual/en/function.readdir.php
		// example #1
		if ($handle = opendir('../user-accounts')) {
			/* This is the correct way to loop over the directory. */
			while (false !== ($entry = readdir($handle))) {
				array_push($useraccts, $entry);
			}
			closedir($handle);
		}
	}
	
	// array -> array
	// makes sure that the accounts in $useraccts are valid names (ie not a .)
	function validateUsers($acctArray){
		$accounts = array();
		foreach($acctArray as $element){
			if($element=="." || $element==".."){
				// do nothing
			} else{
				array_push($accounts, $element);
			}
		}
		 return $accounts;
	}
?>